package mo.leetcode_top;

public class L29 {

    public int divide(int dividend, int divisor) {
        if (dividend == Integer.MIN_VALUE && divisor == -1) {
            return Integer.MAX_VALUE;
        }
        int sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1;
        long m = dividend;
        m = Math.abs(m);
        long n = divisor;
        n = Math.abs(n);
        if (n == 1) {
            return sign == 1 ? (int)m : -(int)m;
        }
        int res = 0;
        while (m  >= n) {
            long t = n;
            long p = 1;
            while (m >= (t << 1)) {
                p = p << 1;
                t = t << 1;
            }
            m -= t;
            res += p;
        }
        return sign == 1 ? res : -res;
    }

    public static void main(String[] args) {
        L29 test = new L29();
        System.out.println(test.divide( -1010369383, -2147483648));
    }
}
